// index.js
var network = require("../../utils/network.js");
var WxParse = require('../../wxParse/wxParse.js'); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryDetail: [],
    properties: [],
    cityArray: [],
    cityIndex: 203,
    projectArray: [],
    projectIndex: 0,
    area: 0
  },
  /**
   * 服务详情
  */
  categoryDetail: function () {
    let that = this;
    let URL = network.server() + 'service/info';
    let Params = { service_id: that.data.service_id };
    let Method = 'GET';
    let Message = '';
    network.requestGet(URL, Params, Method, Message, function (res) {
      console.log('服务详情---', res)
      if (res.code == 0) {
        let categoryDetail = res.data;
        let properties = JSON.parse(res.data.Obj.properties);
        let projectArray = [];
        let list = properties.project_class_list
        for (var key in list) {
          let item = list[key]
          projectArray.push(item)
        }
        console.log(projectArray)
        let article = res.data.Obj.content;
        that.setData({
          categoryDetail: categoryDetail,
          properties: properties,
          projectArray: projectArray,
          content: WxParse.wxParse('article', 'html', article, that, 5)
        })
      }
    }, function () {
      // wx.showToast({
      //   title: '加载数据失败',
      // })
    })
  },
  /**
   * 改变项目类型
  */
  bindProjectChange: function (e) {
    let that = this;
    that.setData({
      projectIndex: e.detail.value
    })
  },
  /**
   * 获取city
  */
  getCity: function () {
    let that = this;
    let URL = network.server() + 'area/my-city-list';
    let page = 1;
    let page_size = 500;
    let Params = { 
      area_name: '',
      page: page,
      page_size: page_size
    };
    let Method = 'GET';
    let Message = '';
    network.requestGet(URL, Params, Method, Message, function (res) {
      console.log('城市列表---', res)
      if (res.code == 0) {
        let cityArrayDemo = []
        for (var i = 0; i < res.data.list.length; ++i) {
          let item = res.data.list[i]
          cityArrayDemo.push(item.area_name)
        }
        that.setData({
          cityArray: cityArrayDemo,
        })
      }
    }, function () {
      // wx.showToast({
      //   title: '加载数据失败',
      // })
    })
  },
  /**
   * 改变城市
  */
  bindCityChange: function(e){
    let that = this;
    that.setData({
      cityIndex: e.detail.value
    })
    console.log(that.data.cityIndex)
  },
  /**
   * 提交
  */
  formSubmit: function (e) {
    var tel = e.detail.value.tel;
    var phoneReg = /^1[34578]\d{9}$/;
    if (e.detail.value.username == '') {
      wx.showModal({
        content: '请输入您的称呼',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.tel == '') {
      wx.showModal({
        content: '请输入手机号码',
        showCancel: false
      })
      return false
    }
    if (!phoneReg.test(tel)) {
      wx.showModal({
        content: '手机号码格式错误',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.address == '') {
      wx.showModal({
        content: '请输入您的小区名称',
        showCancel: false
      })
      return false
    }   
    if (e.detail.value.area == '') {
      wx.showModal({
        content: '请输入您的房屋面积',
        showCancel: false
      })
      return false
    }
    let that = this;
    let URL = network.server() + 'service-order/add';
    let properties = that.data.properties;
    let Params = {};
    Params.service_id = that.data.service_id;
    Params.member_name = properties.member_name==1?e.detail.value.username:'';
    Params.member_mobile = properties.member_mobile == 1 ? e.detail.value.tel : '';
    Params.city_name = properties.city_name == 1 ? that.data.cityArray[that.data.cityIndex] : '';
    Params.address = properties.address == 1 ? e.detail.value.address : '';
    Params.project_class = properties.project_class == 1 ? that.data.projectArray[that.data.projectIndex]: '';
    Params.acreage = properties.acreage == 1 ? e.detail.value.area : '';
    let Method = 'POST';
    let Message = '';
    network.requestPost(URL, Params, Method, Message, function (res) {
      console.log('提交---', res)
      wx.showModal({
        content: '提交成功',
        showCancel: false
      })
    }, function () {
      wx.showToast({
        title: '提交失败',
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var service_id = options.service_id;
    console.log(service_id)
    var service_name = options.service_name;
    this.setData({
      service_id: service_id
    })
    wx.setNavigationBarTitle({
      title: service_name,
    })
    this.categoryDetail();
    this.getCity()
  },
})